#! /usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'huangjacky'
__doc__ = '路由重定向'

from config import config
from base import CGI
from tornado.web import StaticFileHandler
from init import py2
import os
if py2:
    from imp import load_source
else:
    from importlib.machinery import SourceFileLoader
# 对外路由
router = []
__cwd = os.getcwd()
# 首先加载路由目录下面的已经定义好的类
for f in os.listdir(config['router_dir']):
    if not f.endswith('.py') or f.startswith('__'):
        continue
    fn = os.path.join(__cwd, config['router_dir'], f)
    cls_name = f[:-3]
    if py2:
        m = load_source(cls_name, fn)
    else:
        pass
    cls = getattr(m, cls_name)
    if not issubclass(cls, CGI):
        continue
    uri = getattr(cls, 'uri')
    setting = getattr(cls, 'setting')
    if setting and len(setting):
        router.append((uri, cls, setting))
    else:
        router.append((uri, cls))


